package com.prs.crm.dao.crm.impl;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.prs.crm.dao.crm.RecodhaifenDao;
import com.prs.crm.dao.impl.BaseDaoImpl;
import com.prs.crm.domain.crm.Recodhaifen;
import com.prs.crm.domain.crm.mainBill;
import com.prs.crm.service.crm.RecodhaifenService;

public class RecodhaifenDaoImpl extends BaseDaoImpl<Recodhaifen> implements
		RecodhaifenDao {
	private RecodhaifenService recodhaifenService;

	@SuppressWarnings("unchecked")
	@Override
	public Recodhaifen getRecord() {
		return (Recodhaifen) getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException {
						Criteria criteria = session
								.createCriteria(Recodhaifen.class);
						List<Recodhaifen> alist = criteria.list();
						if (alist.size() == 0) {
							return null;
						} else {
							return alist.get(0);
						}

					}
				});
	}

	@Override
	public void updateRecord(String record, Recodhaifen hf) {
		Recodhaifen jh = this.getRecodhaifenService()
				.getRecodhaifen(hf.getId());
		jh.setRecordNum(record);
		this.getHibernateTemplate().saveOrUpdate(jh);

	}

	@Override
	public Recodhaifen getRecodhaifen(Serializable id) {

		return this.getHibernateTemplate().get(Recodhaifen.class, id);
	}

	public RecodhaifenService getRecodhaifenService() {
		return recodhaifenService;
	}

	public void setRecodhaifenService(RecodhaifenService recodhaifenService) {
		this.recodhaifenService = recodhaifenService;
	}

	@SuppressWarnings("unchecked")
	@Override
	public int SerchBill(final String mainBill) {
		return (Integer) getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException {
						Criteria criteria = session
								.createCriteria(mainBill.class);
						criteria.add(Restrictions.eq("billNum", mainBill));
						List<mainBill> alist = criteria.list();

						return alist.size();

					}
				});
	}

	@Override
	public void saveBill(mainBill mb) {
		this.getHibernateTemplate().save(mb);
		
	}

}
